package ljl.alg.wangzheng_camp.round1.dp;

public class _309_max_profit {
    
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][3];
        dp[0][0] = -prices[0];
        // 0 持有股票最大收益 1 不持有但冷冻，刚卖完 2 不持有，不冷冻，可交易
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
            dp[i][1] = dp[i - 1][0] + prices[i];
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);
        }
        return Math.max(dp[n - 1][1], dp[n - 1][2]);
    }
    
    public int maxProfit2(int[] prices) {
        int n = prices.length;
        int hold = -prices[0];
        int coolingDown = 0, readyToBuy = 0;
        // 0 持有股票最大收益 1 不持有但冷冻，刚卖完 2 不持有，不冷冻，可交易
        for (int i = 1; i < n; i++) {
            int hold1 = Math.max(hold, readyToBuy - prices[i]);
            int cool1 = hold + prices[i];
            int ready1 = Math.max(coolingDown, readyToBuy);
            hold = hold1;
            coolingDown = cool1;
            readyToBuy = ready1;
        }
        return Math.max(coolingDown, readyToBuy);
    }
    
}
